package cn.tedu.lbs.basic.web.controller;

import cn.tedu.lbs.basic.web.service.AdminService;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.common.passport.encoder.JwtEncoder;
import com.tarena.lbs.common.passport.principle.UserPrinciple;
import com.tarena.lbs.pojo.basic.vo.AdminVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@Slf4j
public class AdminController {
    //form表单 String id ,String name; User(id,name)
    //json {"id":"","name":""} @RequestBody User(id,name)
    //Session Request Response @Cookie @RequestHeader
    @Autowired
    private AdminService adminService;
    @GetMapping("/admin/basic/role/detail")
    public Result<AdminVO> adminDetail(
            HttpServletRequest request,
            @RequestHeader("Authorization") String jwt) throws BusinessException {
        UserPrinciple userPrinciple = (UserPrinciple) request.getAttribute("userPrinciple");
        log.info("后台用户详情，验证request作用域：{}",userPrinciple);
        log.info("后台用户详情,jwt请求头:{}",jwt);
        //Thread thread = Thread.currentThread();
        //log.info("admin详情查询控制层 执行线程对象:{},id:{}",thread,thread.getId());
        //怎么解析 Authorization
        //1.入参接收request
        //String jwt = request.getHeader("Authorization");
        //2.直接使用注解获取 jwt
        //解析jwt 拿到jwt中携带的数据UserPrinciple
//        UserPrinciple userPrinciple=parseJwt(jwt);
//        Integer id=userPrinciple.getId();
        //调用业务层
        AdminVO vo=adminService.adminDetail();
        log.info("后台用户详情,jwt请求头:{},出参AdminVO:{}",jwt,vo);
        return new Result<>(vo);
    }
    /*@Autowired
    private JwtEncoder<UserPrinciple> jwtDecoder;
    private UserPrinciple parseJwt(String jwt) throws BusinessException {
        //jwt一定存在?
        if (jwt!=null&&jwt.length()>0){
            //核心: 利用jwtEncoder 将jwt转化成UserPrinciple
            return jwtDecoder.getLoginFromToken(jwt,UserPrinciple.class);
        }else{
            throw new BusinessException("-2","Authorization不能为空");
        }
    }*/
}
